package org.shock.ss.sms.action.system;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;

import net.sf.json.JSONArray;
import org.shock.ss.sms.commons.annotation.RightAnno;
import org.shock.ss.sms.commons.db.DbTools;
import org.shock.ss.sms.bean.Page;
import org.shock.ss.sms.bean.Pager;
import org.shock.ss.sms.commons.utils.ParamsUtil;
import org.shock.ss.sms.commons.utils.ReflexUtils;
import org.shock.ss.sms.commons.utils.Util;

import org.shock.ss.sms.bean.Tree;
import org.shock.ss.sms.bean.system.Role;
import org.shock.ss.sms.bean.system.SysPortal;

import org.shock.ss.sms.commons.dao.DaoFactory;
import org.shock.ss.sms.dao.system.impl.SysPortalDaoImpl;
import org.shock.ss.sms.service.system.SysPortalService;
import org.shock.ss.sms.service.system.impl.SysPortalServiceImpl;
import org.shock.ss.sms.action.BaseAction;

/**
 * 面板管理action
 * 
 * @author jile
 * 
 */
public class SysPortalAction extends BaseAction {
	private static final long serialVersionUID = 1L;
	/** 获得service */
	private SysPortalService ps = new SysPortalServiceImpl((SysPortalDaoImpl) DaoFactory.getDao("SysPortalDao"));
	private SysPortal sysportal = new SysPortal();
	private List list = new ArrayList();
	private Map map = new HashMap();
	private Long roleid;
	private Integer node;
	private StringBuffer portalString = new StringBuffer();
	private StringBuffer selectString1 = new StringBuffer();
	private StringBuffer selectString2 = new StringBuffer();
	private StringBuffer arr = new StringBuffer();

	public String portal() throws Exception {
		if (null == getUserInfor().getIsadmin() || getUserInfor().getIsadmin() != 1) {
			String ids = DbTools.getString("select portalids from ckgl_role where id in ("
					+ getUserInfor().getRoleids() + ")");
			sqladd.append("and id in(" + (ids.length() == 0 ? 0 : ids) + ")");
		}
		String sql = "select * from " + SysPortal.getTable() + " where 1=1 " + sqladd;
		list = ps.listAll(sql);
		int i = 0;
		for (Map map : (ArrayList<Map>) list) {
			portalString
					.append("Portal"
							+ map.get("id")
							+ "View = Ext.extend(Ext.ux.Portlet, {tools : null,constructor : function(a) {Ext.applyIf(this, a);this.initTool();Portal"
							+ map.get("id")
							+ "View.superclass.constructor.call(this, {id : \"Portal"
							+ map.get("id")
							+ "View\",title : \""
							+ map.get("title")
							+ "\",iconCls : \""
							+ map.get("icon")
							+ "\",tools : this.tools,border : false,autoLoad : {url : _ctxPath + \""
							+ map.get("url")
							+ "\",scripts : true}});},initTool : function() {this.tools = [{id : \"refresh\",handler : function() {Ext.getCmp(\"Portal"
							+ map.get("id")
							+ "View\").getUpdater().update({url : _ctxPath+ \""
							+ map.get("url")
							+ "\",scripts : true});}}, {id : \"close\",handler : function(c, b, a) {Ext.Msg.confirm(\"提示信息\", \"确认删除此模块吗？\",function(d) {if (d == \"yes\") {a.ownerCt.remove(a, true);}});}}];}});");
			if (i <= list.size() / 2) {
				selectString1.append("{xtype : \"checkbox\",boxLabel : \"" + map.get("title")
						+ "\",hideLabel : true,id : \"Portal" + map.get("id") + "ViewCheckBox\",name : \""
						+ map.get("title") + "\"},");
			} else {
				selectString2.append("{xtype : \"checkbox\",boxLabel : \"" + map.get("title")
						+ "\",hideLabel : true,id : \"Portal" + map.get("id") + "ViewCheckBox\",name : \""
						+ map.get("title") + "\"},");
			}
			arr.append("\"Portal" + map.get("id") + "View\",");
			System.out.println(arr);
			i++;
		}

		if (arr.lastIndexOf(",") > 0) {
			arr.deleteCharAt(arr.lastIndexOf(","));
		}
		if (selectString1.lastIndexOf(",") > 0) {
			selectString1.deleteCharAt(selectString1.lastIndexOf(","));
		}
		if (selectString2.lastIndexOf(",") > 0) {
			selectString2.deleteCharAt(selectString2.lastIndexOf(","));
		}
		System.out.println(portalString);
		return SUCCESS;
	}

	public String viewSysPortal() throws Exception {
		return SUCCESS;
	}

	/**
	 * 增加或更新面板 根据bean 的id进行判断，如果id为null则为增加，否则为更新。
	 * 
	 * @return
	 * @throws Exception
	 */
	public String saveSysPortal() throws Exception {
		BeanUtils.populate(sysportal, map);
		// System.out.println(ReflexUtils.rwritertoString(sysportal));
		if (sysportal.getId() == null || sysportal.getId() == 0) {
			addSysPortal();
		} else {
			updateSysPortal();
		}
		return SUCCESS;
	}

	@RightAnno(rightValue = "addSysPortal", rightNote = "面板添加")
	public String addSysPortal() throws Exception {
		log.debug(ReflexUtils.rwritertoString(sysportal));// 调试bean信息
		if (action.equals("before")) {// 显示添加页面
			return "before";
		}
		ps.add(sysportal);
		message = "添加成功！";
		jmpUrl = "listSysPortal.do";
		return SUCCESS;
	}

	@RightAnno(rightValue = "updateSysPortal", rightNote = "修改面板")
	public String updateSysPortal() throws Exception {
		if (action.equals("before")) {// 显示添加页面
			String sql = "select * from " + SysPortal.getTable() + " where id=" + sysportal.getId();
			map = ps.view(sql);
			BeanUtils.populate(sysportal, map);
			return "before";
		}
		ps.update(sysportal);
		message = "更新成功！";
		jmpUrl = "listSysPortal.do";
		return SUCCESS;
	}

	@RightAnno(rightValue = "listSysPortal", rightNote = "面板列表")
	public String listSysPortal() throws Exception {
		/** 查询条件 */
		if (null != sysportal) {
			if (!Util.isEmpty(sysportal.getTitle())) {
				sqladd.append("and title like '%" + sysportal.getTitle().trim() + "%'");
			}
			if (!Util.isEmpty(sysportal.getPortalid())) {
				sqladd.append(" and portalid like '%" + sysportal.getPortalid().trim() + "%'");
			}
		}
		Pager pager = new Pager();
		String sul = ParamsUtil.transParams(getRequest().getParameterMap());
		String sqlcount = "select count(1) from " + SysPortal.getTable() + " where 1=1 " + sqladd;
		String sql = "select * from " + SysPortal.getTable() + " where 1=1 " + sqladd;
		// System.out.println(sql);
		pager = ps.list("asc", "id", pageno, rownum, sqlcount, sql);
		list = pager.getList();
		Page page = new Page();
		page.setPageString(pager.getTotalCount(), rownum, pageno, pagerow, sul);
		pageStr = page.getPageString();
		return SUCCESS;
	}

	@RightAnno(rightValue = "delSysPortal", rightNote = "删除面板")
	public String delSysPortal() throws Exception {
		String sql = "delete from ckgl_SysPortal where id =" + sysportal.getId();
		ps.del(sql);
		message = "删除成功！";
		jmpUrl = "listSysPortal.do";
		return SUCCESS;
	}

	/** 用于产生角色选择树，供Ext选择 */
	public String genSysPortalTree() throws Exception {
		String sql = "select title,id from " + SysPortal.getTable() + " order by id";
		list = DbTools.getMapsInList(sql);
		List tmpList = new ArrayList();
		for (Map map : (ArrayList<Map>) list) {
			Tree tree = new Tree();
			tree.setId((String) map.get("id"));
			tree.setText((String) map.get("title"));
			tree.setLeaf(true);
			tree.setChecked(false);
			tmpList.add(tree);
		}
		jsonString = JSONArray.fromObject(tmpList).toString();
		// System.out.println(jsonString);
		return SUCCESS;
	}

	public String listAllSysPortal() throws Exception {
		if (roleid != null) {
			sqladd.append("and id in (select sysportalids from " + Role.getTable() + " where id=" + roleid + ")");
			list = DbTools.getList("select title,id from " + SysPortal.getTable() + " where 1=1 " + sqladd);
			jsonString = JSONArray.fromObject(list).toString();
			return SUCCESS;
		}
		String sql = "select title,id from " + SysPortal.getTable() + " where modid= " + node + " order by id";
		list = DbTools.getMapsInList(sql);
		List tmpList = new ArrayList();
		for (Map map : (ArrayList<Map>) list) {
			Tree tree = new Tree();
			tree.setCode("r" + Long.parseLong((String) map.get("id")));
			tree.setText((String) map.get("title"));
			tree.setLeaf(true);
			tree.setChecked(false);
			tmpList.add(tree);
		}
		jsonString = JSONArray.fromObject(tmpList).toString();
		// System.out.println(jsonString);
		return SUCCESS;
	}

	public SysPortal getSysportal() {
		return sysportal;
	}

	public void setSysportal(SysPortal sysportal) {
		this.sysportal = sysportal;
	}

	public List getList() {
		return list;
	}

	public void setList(List list) {
		this.list = list;
	}

	public Map getMap() {
		return map;
	}

	public void setMap(Map map) {
		this.map = map;
	}

	public Long getRoleid() {
		return roleid;
	}

	public void setRoleid(Long roleid) {
		this.roleid = roleid;
	}

	public Integer getNode() {
		return node;
	}

	public void setNode(Integer node) {
		this.node = node;
	}

	public StringBuffer getPortalString() {
		return portalString;
	}

	public void setPortalString(StringBuffer portalString) {
		this.portalString = portalString;
	}

	public StringBuffer getSelectString1() {
		return selectString1;
	}

	public void setSelectString1(StringBuffer selectString1) {
		this.selectString1 = selectString1;
	}

	public StringBuffer getSelectString2() {
		return selectString2;
	}

	public void setSelectString2(StringBuffer selectString2) {
		this.selectString2 = selectString2;
	}

	public StringBuffer getArr() {
		return arr;
	}

	public void setArr(StringBuffer arr) {
		this.arr = arr;
	}

}
